/*
 * @Author: hifishroom
 * @Date: 2021-08-26 10:57:13
 * @LastEditTime: 2021-09-03 12:53:18
 * @FilePath: \新建文件夹\src\store\module\menusModule.js
 */
let menus = [
  {
    id: 1,
    level: 1,
    name: "系统首页",
    type: "link",
    url: "/background/index"
  },
  {
    id: 2,
    level: 1,
    name: "账户管理",
    type: "button",
    isExpanded: false,
    isSelected: false,
    subMenu: [
      {
        id: 22,
        level: 2,
        name: "角色列表",
        type: "link",
        url: "/background/account/super"
      },
      {
        id: 23,
        level: 2,
        name: "用户管理",
        type: "link",
        url: "/background/account/admin"
      },
      {
        id: 24,
        level: 2,
        name: "普通用户",
        type: "link",
        url: "/background/account/user"
      }
    ]
  },
  {
    id: 3,
    level: 1,
    name: "坐标信息管理",
    type: "button",
    isExpanded: false,
    isSelected: false,
    subMenu: [
      {
        id: 31,
        level: 2,
        name: "基础信息",
        type: "link",
        url: "/background/information/basicInformation"
      },
      {
        id: 32,
        level: 2,
        name: "类型信息",
        type: "button",
        isExpanded: false,
        isSelected: false,
        subMenu: [
          {
            id: 321,
            level: 3,
            name: "营房信息",
            type: "link",
            url: "/background/information/detail/barracks"
          },
          {
            id: 322,
            level: 3,
            name: "酒店信息",
            type: "link",
            url: "/background/information/detail/hotel"
          },
          {
            id: 323,
            level: 3,
            name: "靶场信息",
            type: "link",
            url: "/background/information/detail/range"
          },
          {
            id: 324,
            level: 3,
            name: "仓储信息",
            type: "link",
            url: "/background/information/detail/storange"
          },
          {
            id: 325,
            level: 3,
            name: "养殖场信息",
            type: "link",
            url: "/background/information/detail/farm"
          }
        ]
      },
      {
        id: 38,
        level: 2,
        name: "历史沿革",
        type: "link",
        url: "/background/information/detail/history"
      },
      {
        id: 33,
        level: 2,
        name: "楼栋信息",
        type: "link",
        url: "/background/information/tower"
      },
      {
        id: 34,
        level: 2,
        name: "设备信息",
        type: "link",
        url: "/background/information/facility"
      },
      {
        id: 35,
        level: 2,
        name: "遗留问题信息",
        type: "link",
        url: "/background/information/leftoverProblem"
      },
      {
        id: 36,
        level: 2,
        name: "电梯信息",
        type: "link",
        url: "/background/information/elevator"
      },
      {
        id: 37,
        level: 2,
        name: "工程信息",
        type: "link",
        url: "/background/information/project"
      }
    ]
  },
  {
    id: 4,
    level: 1,
    name: "字典",
    type: "button",
    isExpanded: false,
    isSelected: false,
    subMenu: [
      {
        id: 41,
        level: 2,
        name: "手动查询",
        type: "link",
        url: "/background/childsContent/dictionaries/dictionaries"
      }
    ]
  }
];
let levelNum = 1;
let startExpand = []; // 保存刷新后当前要展开的菜单项
function setExpand(source, url) {
  let sourceItem = "";
  for (let i = 0; i < source.length; i++) {
    sourceItem = JSON.stringify(source[i]); // 把菜单项转为字符串
    if (sourceItem.indexOf(url) > -1) {
      // 查找当前 URL 所对应的子菜单属于哪一个祖先菜单
      if (source[i].type === "button") {
        // 导航菜单为按钮
        source[i].isSelected = true; // 设置选中高亮
        source[i].isExpanded = true; // 设置为展开
        startExpand.push(source[i]);
        // 递归下一级菜单，以此类推
        setExpand(source[i].subMenu, url);
      }
      break;
    }
  }
}
const state = {
  menus,
  levelNum
};
const mutations = {
  findParents(state, payload) {
    if (payload.menu.type === "button") {
      payload.menu.isExpanded = !payload.menu.isExpanded;
    } else if (payload.menu.type === "link") {
      if (startExpand.length > 0) {
        for (let i = 0; i < startExpand.length; i++) {
          startExpand[i].isSelected = false;
        }
      }
      startExpand = []; // 清空展开菜单记录项
      setExpand(state.menus, payload.menu.url);
    }
  },
  firstInit(state, payload) {
    setExpand(state.menus, payload.url);
  }
};
export default {
  state,
  mutations
};
